package org.dreamwork.smart.home.remote;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import org.dreamwork.smart.home.remote.wifi.RemoteCar;

import java.io.IOException;

/**
 * Created by seth.yang on 2015/6/15
 */
public class Splash extends Activity implements Runnable, View.OnClickListener {
    private Handler handler;

    private Button btnFind;
    private TextView tip;
    private ProgressBar bar;

    private static final int FLAG_FOUND = 0, FLAG_NOT_FOUND = 1;

    @Override
    public void onCreate (Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.splash);

        handler = new MyHandler (this);

        btnFind = (Button) findViewById (R.id.btnFindAgain);
        tip     = (TextView) findViewById (R.id.txtMessage);
        bar     = (ProgressBar) findViewById (R.id.progressBar);

        btnFind.setOnClickListener (this);

        findRemoteCar ();
    }

    @Override
    public void run () {
        RemoteCar car = null;
        try {
            car = RemoteCar.find (8001);
        } catch (IOException ex) {
            Log.w (ex.getMessage (), ex);
        }

        if (car != null) {
            Message msg = handler.obtainMessage (FLAG_FOUND, car);
            handler.sendMessage (msg);
        } else {
            handler.sendEmptyMessage (FLAG_NOT_FOUND);
        }
    }

    private void findRemoteCar () {
        bar.setVisibility (View.VISIBLE);
        tip.setText (R.string.finding);
        btnFind.setVisibility (View.GONE);

        new Thread (this).start ();
    }

    private void showError () {
        bar.setVisibility (View.GONE);
        tip.setText (R.string.tip_find_again);
        btnFind.setVisibility (View.VISIBLE);
    }

    @Override
    public void onClick (View v) {
        switch (v.getId ()) {
            case R.id.btnFindAgain:
                findRemoteCar ();
                break;
        }
    }

    private static final class MyHandler extends Handler {
        Splash splash;

        MyHandler (Splash splash) {
            this.splash = splash;
        }

        @Override
        public void handleMessage (Message msg) {
            switch (msg.what) {
                case FLAG_FOUND :
                    RemoteCar car = (RemoteCar) msg.obj;
                    Intent intent = new Intent (splash, ControlPanel.class);
                    intent.putExtra ("ip", car.getIp ().getHostAddress ());
                    intent.putExtra ("control", car.getControlPort ());
                    intent.putExtra ("camera", car.getCameraPort ());
                    splash.startActivity (intent);
                    splash.finish ();
                    break;
                case FLAG_NOT_FOUND :
                    splash.showError ();
                    break;
            }
        }
    }
}